<!-- user表单弹窗 -->
<form id="user-form" lay-filter="user-form" class="layui-form model-form">
	<input name="userId" type="hidden" />
	<div class="layui-form-item">
		<label class="layui-form-label">用户名</label>
		<div class="layui-input-block">
			<input name="username" placeholder="请输入账号" type="text" class="layui-input" maxlength="20" lay-verify="required" required/>
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label">密码</label>
		<div class="layui-input-block">
			<input name="passd" id="ps" placeholder="密码" type="text" class="layui-input" required maxlength="10" />
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">手机号</label>
		<div class="layui-input-block">
			<input name="mobile" placeholder="请输入手机号" type="text" class="layui-input" required/>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label">角色</label>
		<div class="layui-input-block">
			<select id="rolejs" name="roleId" lay-verify="required">
			</select>
		</div>
	</div>

	<div class="layui-form-item">
		<label class="layui-form-label layui-form" lay-filter="selFilter">机构</label>
		<div class="layui-input-block">
			<select id="key" name="orgId">

			</select>
		</div>
	</div>

	<div class="layui-form-item model-form-footer">
		<button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
		<button class="layui-btn" lay-filter="user-form-submit" lay-submit>保存</button>
	</div>
</form>

<script>
	layui.use(['layer', 'admin', 'form', 'formSelects'], function() {
		var layer = layui.layer;
		var admin = layui.admin;
		var form = layui.form;
		var formSelects = layui.formSelects;

		form.render('radio');

		// 获取所有角色
		layer.load(2);

		$.ajax({
			type: "POST",
			url: path + '/sys/role/list1',
			data: {
				sortOrder: "asc",
				pageSize: 1000,
				pageNumber: 1,
				roleName: ''
			},
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			beforeSend: function(request) {},
			success: function(data) {
				// 渲染多选下拉框
				layer.closeAll('loading');
				var select = document.getElementById("rolejs")
				select.options.add(new Option("请选择", ''))
				for(var i = 0; i < data.rows.length; i++) {
					select.options.add(new Option(data.rows[i].roleName, data.rows[i].roleId));
				}
				form.render(select);
				var user = admin.getTempData('t_user');
				$('#user-form').attr('method', 'GET');
				if(user) {
					form.val('user-form', user);
					$('#user-form').attr('method', 'GET');

					$.ajax({
						type: "POST",
						url: path + '/sys/user/infoUser',
						data: {
							userId: user.userId
						},
						dataType: "json",
						xhrFields: {
							withCredentials: true
						},
						beforeSend: function(request) {},
						success: function(data) {
							console.log(data)
							$("#rolejs").val(data.rows.roleIdList[0]);
							form.render('select')
						},
						beforeSend: function() {},
						complete: function() {},

						error: function(result) {
							console.log(result);
						},
					})

				}
			},
			error: function(result) {
				console.log(result);
			},
		})

		$.ajax({
			type: "POST",
			url: path + '/sys/org/list1',
			data: {
				sortOrder: "asc",
				pageSize: 1000,
				pageNumber: 1,
				roleName: ''
			},
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			beforeSend: function(request) {},
			success: function(data) {
				// 渲染多选下拉框
				$('#user-form')[0].reset();
				$('#user-form').attr('method', 'GET');
				var select = document.getElementById("key")
				select.options.add(new Option("请选择", ''))
				for(var i = 0; i < data.length; i++) {
					select.options.add(new Option(data[i].name, data[i].orgId));
				}
				form.render(select);
				var user = admin.getTempData('t_user');
				if(user) {
					var select = document.getElementById("key")
					$(select).val(user.orgId);
					form.val('user-form', user);
				}
				$("#ps").val("******")
			},
			beforeSend: function() {},
			complete: function() {},

			error: function(result) {
				console.log(result);
			},
		})

		// 表单提交事件
		form.on('submit(user-form-submit)', function(data) {

			console.log(data)
			if(data.field.passd == "******" || data.field.passd.length == 0) {
				data.field.passd = ""
			} else {
				if(checkValidPasswd(data.field.passd) == 1) {
					return false;
				}
			}
			var hhh = $(".zbyhhh .layui-layer-title").text();

			if(hhh == "添加用户") {
				auser(data.field)
			} else if(hhh == "修改用户") {
				upuser(data.field);
			}

			return false

		});

		function checkValidPasswd(str) {
			var reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,15}$/;
			if(!reg.test(str)) {
				layer.msg('密码必须包含大小写字母和数字的组合，可以使用特殊字符，长度在6-15之间', {
					icon: 2
				});
				return 1;
			}
			return 0;

		}

		function auser(datas) {
			$.ajax({
				type: "POST",
				url: userSave,
				traditional: true,
				data: {
					username: datas.username,
					password: datas.passd,
					mobile: datas.mobile,
					roleId: datas.roleId,
					orgId: datas.orgId,
					roleIdList: datas.roleId.split(',')
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					if(data.code == 0) {
						layer.closeAll();
						layer.msg(data.msg + "请手动刷新", {
							icon: 1
						});
					} else if(data.code == 500) {
						layer.msg("数据库已存在该员工", {
							icon: 2
						});
					}else{
						layer.msg(data.msg, {
							icon: 2
						});
					}
				},
				complete: function() {},
				error: function(result) {
					console.log(result);
				},
			});
		}

		function upuser(datas) {
			$.ajax({
				type: "POST",
				url: userUpdate,
				traditional: true,
				data: {
					userId: datas.userId,
					username: datas.username,
					password: datas.passd,
					mobile: datas.mobile,
					roleId: datas.roleId,
					orgId: datas.orgId,
					roleIdList: datas.roleId.split(',')
				},
				dataType: "json",
				xhrFields: {
					withCredentials: true
				},
				success: function(data) {
					if(data.code == 0) {
						layer.closeAll();
						layer.msg(data.msg, {
							icon: 1
						});
					} else {
						layer.msg(data.msg, {
							icon: 2
						});
					}
					console.log(data)
				},
				complete: function() {},
				error: function(result) {
					console.log(result);
				},
			});
		}

	});
</script>